package com.tgpms.web.earlierStage.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.entity.DisputeItems;
import com.tgpms.web.earlierStage.service.EarlierDisItemService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "EarlierDisItemController", tags = "前期 争议点相关接口")
@RestController
@RequestMapping(value = "/ev/earlierDisItem")
public class EarlierDisItemController {
	
	@Autowired
	private EarlierDisItemService earlierDisItemService;
	@Autowired
	private TokenService tokenService;

	@ApiOperation(value = "新增争议点接口", notes = "新增争议点接口", httpMethod = "POST")
	@PostMapping(value = "/addDisItem")
	public Result addDisItem(@RequestBody DisputeItems disItem,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(disItem.getTaskId()) || Common.isEmpty(disItem.getDisputeName())
				|| Common.isEmpty(disItem.getStatus()) || Common.isEmpty(disItem.getDisputeFrom())
				|| Common.isEmpty(disItem.getDisputeResult()) || Common.isEmpty(disItem.getRemarks()) ) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			disItem.setInsertUser(insertName);
			result = earlierDisItemService.addDisItem(disItem, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	
	@ApiOperation(value = "修改争议点接口", notes = "修改争议点接口", httpMethod = "PUT")
	@PutMapping(value = "/updateDisItem")
	public Result updateDisItem(@RequestBody DisputeItems disItem,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(disItem.getId())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			disItem.setModifyUser(insertName);
			result = earlierDisItemService.updateDisItem(disItem, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	@ApiOperation(value = "删除争议点接口", notes = "删除争议点接口", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteDisItem")
	public Result deleteDisItem(@RequestParam(value = "id", required = true) String id,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(id)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			result = earlierDisItemService.deleteDisItem(id, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}
	
}
